package com.zoshine.zzg.service;

import com.zoshine.zzg.dao.FamilyMemberOrderDao;
import com.zoshine.zzg.dao.FamilyMemberOrderDetailDao;
import com.zoshine.zzg.dao.GoodsPublishDetailDao;
import com.zoshine.zzg.exception.AppException;
import com.zoshine.zzg.exception.ErrorMessage;
import com.zoshine.zzg.pojo.FamilyMemberOrder;
import com.zoshine.zzg.pojo.FamilyMemberOrderDetail;
import com.zoshine.zzg.pojo.GoodsPublishDetail;
import com.zoshine.zzg.pojo.pojostatus.FamilyMemberOrderStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 贾俊峰
 * @time 2017/8/5 14:41
 */
@Service
@Transactional
public class FamilyMemberOrderService extends BaseService<FamilyMemberOrder> {
    
    private GoodsPublishDetailDao goodsHistoryDetailDao;
    
    private FamilyMemberOrderDao familyMemberOrderDao;

    private FamilyMemberOrderDetailDao familyMemberOrderDetailDao;
    @Autowired
    public void setGoodsHistoryDetailDao(GoodsPublishDetailDao goodsHistoryDetailDao) {
        this.goodsHistoryDetailDao = goodsHistoryDetailDao;
    }

    @Autowired
    public void setFamilyMemberOrderDetailDao(FamilyMemberOrderDetailDao familyMemberOrderDetailDao) {
        this.familyMemberOrderDetailDao = familyMemberOrderDetailDao;
    }
    @Autowired
    public void setFamilyMemberOrderDao(FamilyMemberOrderDao familyMemberOrderDao) {
        this.familyMemberOrderDao = familyMemberOrderDao;
    }

    public void cancelOrder(Long id) {
        if (id == null){
            throw new AppException(ErrorMessage.ORDER_PRISONER_ID_CAN_NOT_NULL);
        }
        FamilyMemberOrder familyMemberOrder = familyMemberOrderDao.selectByPrimaryKey(id);
        if (familyMemberOrder == null){
            throw new AppException(ErrorMessage.FAMILY_MEMBER_ORDER_NOT_FIND);
        }
        if (!(familyMemberOrder.getOrderStatus().equals(FamilyMemberOrderStatus.INIT.getValue()))){
            throw new AppException(ErrorMessage.FAMILY_MEMBER_ORDER_STATUS_NOT_CORRECT);
        }
        //改变状态为撤销状态
        familyMemberOrder.setOrderStatus(FamilyMemberOrderStatus.REVOKE.getValue());
        familyMemberOrderDao.update(familyMemberOrder);
    }

    public void payOrder(Long id, Integer payType) {
        if (id == null){
            throw new AppException(ErrorMessage.ORDER_PRISONER_ID_CAN_NOT_NULL);
        }
        if (payType == null){
            throw new AppException(ErrorMessage.PAY_NOT_CORRECT);
        }
        FamilyMemberOrder familyMemberOrder = familyMemberOrderDao.selectByPrimaryKey(id);
        if (familyMemberOrder == null){
            throw new AppException(ErrorMessage.FAMILY_MEMBER_ORDER_NOT_FIND);
        }
        if (!(familyMemberOrder.getOrderStatus().equals(FamilyMemberOrderStatus.INIT.getValue()))){
            throw new AppException(ErrorMessage.FAMILY_MEMBER_ORDER_PAY_NOT_CORRECT);
        }
        familyMemberOrder.setPayType(payType);
        familyMemberOrder.setOrderStatus(FamilyMemberOrderStatus.AUDIT_SEND.getValue());
        familyMemberOrderDao.update(familyMemberOrder);
    }

    public List<FamilyMemberOrderDetail> listOrderDetail(Long orderId) {
        if (orderId == null){
            throw new AppException(ErrorMessage.ORDER_PRISONER_ID_CAN_NOT_NULL);
        }
        FamilyMemberOrderDetail familyMemberOrderDetail=new FamilyMemberOrderDetail();
        familyMemberOrderDetail.setOrderId(orderId);
        List<FamilyMemberOrderDetail> familyMemberOrderDetails = familyMemberOrderDetailDao.selectList(familyMemberOrderDetail);
        return familyMemberOrderDetails;
    }
    public FamilyMemberOrder addFamilyMemberOrder(FamilyMemberOrder familyMemberOrder){
        if (null == familyMemberOrder.getFamilyMemberId()){
            throw new AppException(ErrorMessage.FAMILY_MEMBER_ID_NOT_NULL);
        }
        if (null == familyMemberOrder.getPrisonerId()){
            throw new AppException(ErrorMessage.ACCOUNT_PRISONER_ID_CAN_NOT_NULL);
        }
        if (familyMemberOrder.getOrderDetailList()==null || familyMemberOrder.getOrderDetailList().size()==0){
            throw  new AppException(ErrorMessage.FAMILY_MEMBER_GOODS_NOT_NULL);
        }
        //新增家属点单
        Integer totalPrice=0;
        familyMemberOrder.setId(super.baseIdWorker.createId());
        familyMemberOrder.setOrderStatus(FamilyMemberOrderStatus.INIT.getValue());
        //新增家属点单详情
        List<FamilyMemberOrderDetail> familyMemberOrderDetails=new ArrayList<>();
        for (FamilyMemberOrderDetail familyMemberOrderDetail:familyMemberOrder.getOrderDetailList()){
            if (familyMemberOrderDetail.getGoodsId()==null){
                throw new AppException(ErrorMessage.GOODS_ID_CAN_NOT_BE_NULL_OR_UNDER_ZERO);
            }
            if (familyMemberOrderDetail.getGoodsNum() == null){
                throw new AppException(ErrorMessage.GOODS_COUNT_NOT_BE_NULL);
            }
            GoodsPublishDetail goodsPublishDetail = goodsHistoryDetailDao.selectByPrimaryKey(familyMemberOrderDetail.getGoodsId());
            if (goodsPublishDetail == null){
                throw new AppException(ErrorMessage.GOODS_NOT_EXIST);
            }
            familyMemberOrderDetail.setId(super.baseIdWorker.createId());
            familyMemberOrderDetail.setOrderId(familyMemberOrder.getId());
            familyMemberOrderDetail.setTotalPrice(familyMemberOrderDetail.getGoodsNum()*goodsPublishDetail.getGoodsSellPrice());
            familyMemberOrderDetails.add(familyMemberOrderDetail);
            totalPrice+=familyMemberOrderDetail.getTotalPrice();
        }
        familyMemberOrder.setTotalPrice(totalPrice);
        familyMemberOrder.setCreateTime(new Date());

        //保存数据
        familyMemberOrderDao.insert(familyMemberOrder);
        familyMemberOrderDetailDao.batchInsert(familyMemberOrderDetails);
        return  familyMemberOrder;
    }
}
